import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import RoleView from '../views/RoleView.vue'
import HomeMain from '@/components/home/HomeMain.vue'

import GridorView from '@/views/GridorView.vue'

import FeedbackView from '@/views/FeedbackView.vue'

import FeedbackDetail from '@/components/feedback/FeedbackDetail.vue'

import Login from '@/components/home/Login.vue'

import FeedbackPublic from '@/components/feedback/FeedbackPublic.vue'

import FeedbackUndo from '@/components/feedback/FeedbackUndo.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/nepmhome',
      name: 'nepmhome',
      component: HomeView,
      redirect: '/user',
      children: [
        {
          path: '/user',
          redirect: '/users',
          children: [
            {
              path: '/users',
              name: 'users',
              component: HomeMain
            }
          ]
        },
        {
          path: '/role',
          redirect: '/roles',
          children: [
            {
              path: '/roles',
              name: 'roles',
              component: RoleView
            }
          ]
        },
        {
          path: '/gridor',
          name: 'gridor',
          component: GridorView
        },
        {
          path: '/feedbacks',
          name: 'feedbacks',
          component: FeedbackView
        },
        {
          path: '/detail',
          name:'detail',
          component: FeedbackDetail
        },
        {
          path: '/public',
          name:'public',
          component: FeedbackPublic
        },
        {
          path: '/undo',
          name:'undo',
          component: FeedbackUndo
        }
      ]
    }
  ]
})


//路由守卫
const whiteList=['/login']
router.beforeEach((to,from,next)=>{
  document.title=to.meta.title
  if(!whiteList.includes(to.path)){
    if(!localStorage.getItem('token')){
      router.push('/login')
      return
    }
    next()
    return
  }
  next()
})

export default router
